package net.hn.hnms.biz.blueprint.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.hn.hnms.biz.blueprint.domain.FileInfo;
import net.hn.hnms.biz.blueprint.domain.FileTypeInfo;
import net.hn.hnms.biz.blueprint.request.*;
import net.hn.hnms.biz.blueprint.request.*;
import net.hn.hnms.biz.blueprint.service.ICatalogService;
import net.hn.hnms.biz.blueprint.service.IFileInfoRecordService;
import net.hn.hnms.biz.blueprint.service.IFileInfoService;
import net.hn.hnms.biz.blueprint.vo.CatalogVO;
import net.hn.hnms.biz.blueprint.vo.FileInfoPageVO;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.web.controller.BaseController;
import net.hn.hnms.sys.common.log.annotation.Log;
import net.hn.hnms.sys.common.log.enums.BusinessType;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Map;

/**
 * @author Wang Jingrui
 * @date 2022/9/13 14:28
 */
@Api(value = "图纸文档管理", tags = {"图纸文档管理"})
@RestController
@RequestMapping("/blueprint")
public class BlueprintController extends BaseController {

    @Autowired
    private IFileInfoService fileInfoService;

    @Autowired
    private IFileInfoRecordService fileInfoRecordService;

    @Autowired
    private ICatalogService catalogService;

    /**
     * 文件历史列表
     */
    @ApiOperation("文件历史列表")
//    @SaCheckPermission("blueprint:record:page")
    @GetMapping("/record/page")
    @AutoSetMineCode
    public R<ResponsePage<FileInfoPageVO>> page(@NotBlank(message = "文件ID不能为空")
                                                @RequestParam(value = "fileInfoId") String fileInfoId,
                                                PageQuery pageQuery,@ApiParam(value = "煤矿编码") String mineCode,@ApiParam(value = "公司编码") String companyCode) {
        return R.ok(fileInfoRecordService.selectPageList(fileInfoId, pageQuery,mineCode,companyCode));
    }

    /**
     * 文件列表
     */
    @ApiOperation("文件列表")
//    @SaCheckPermission("blueprint:info:page")
    @GetMapping("/info/page")
    @AutoSetMineCode
    public R<ResponsePage<FileInfoPageVO>> page(FileInfoReq req, PageQuery pageQuery) {
        return R.ok(fileInfoService.selectPageList(req, pageQuery));
    }

    /**
     * 上传或版本更新
     */
    @ApiOperation("上传或版本更新")
    @SaCheckPermission("blueprint:file:update")
    @PostMapping("/file/save")
    @Log(title = "上传或版本更新", businessType = BusinessType.OTHER)
    public R<Void> save(@RequestBody @Validated FileSaveReq req) {
        fileInfoService.save(req);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @SaCheckPermission("blueprint:file:delete")
    @DeleteMapping("/file/delete")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    public R<Void> delete(@RequestBody @Validated FileInfoDeleteReq req) {
        fileInfoService.delete(req);
        return R.ok();
    }

    /**
     * 页面字典
     */
    @ApiOperation("页面字典")
//    @SaCheckPermission("blueprint:type:list")
    @GetMapping("/dict/list")
    @AutoSetMineCode
    public R<List<Map<String, Object>>> dictList(@ApiParam(value = "煤矿编码") String mineCode,@ApiParam(value = "公司编码") String companyCode) {
        return R.ok(fileInfoService.dictList(mineCode,companyCode));
    }
    /**
     * 页面字典
     */
    @ApiOperation("页面字典-文件分类数据字典")
//    @SaCheckPermission("blueprint:type:list")
    @GetMapping("/dict/fileTypelist")
    @AutoSetMineCode
    public R<List<FileTypeInfo>> dictFileTypelist(@ApiParam(value = "煤矿编码") String mineCode, @ApiParam(value = "公司编码") String companyCode) {
        return R.ok(fileInfoService.dictFileTypelist(mineCode,companyCode));
    }

    /**
     * 目录树
     */
    @ApiOperation("目录树")
//    @SaCheckPermission("blueprint:catalog:update")
    @GetMapping("/catalog/tree")
    public R<List<CatalogVO>> treeCatalog() {
        return R.ok(catalogService.treeCatalog());
    }

    /**
     * 目录列表
     */
    @ApiOperation("目录列表")
//    @SaCheckPermission("blueprint:catalog:update")
    @GetMapping("/catalog/list")
    public R<List<CatalogVO>> listCatalog() {
        return R.ok(catalogService.listCatalog());
    }

    /**
     * 保存目录
     */
    @ApiOperation("保存目录")
    @SaCheckPermission("blueprint:catalog:save")
    @PostMapping("/catalog/save")
    public R<Void> saveCatalog(@RequestBody @Validated CatalogReq req) {
        catalogService.saveCatalog(req);
        return R.ok();
    }

    /**
     * 设置目录权限
     */
    @ApiOperation("设置目录权限")
    @SaCheckPermission("blueprint:catalog:update")
    @PostMapping("/catalog/permission")
    @Log(title = "设置目录权限", businessType = BusinessType.GRANT)
    public R<Void> authorizeCatalog(@RequestBody @Validated CatalogPermissionReq req) {
        catalogService.authorizeCatalog(req);
        return R.ok();
    }

    /**
     * 删除目录
     */
    @ApiOperation("删除目录")
    @SaCheckPermission("blueprint:catalog:delete")
    @DeleteMapping("/catalog/{catalogIds}")
    @Log(title = "删除目录", businessType = BusinessType.DELETE)
    public R<Void> deleteCatalog(@NotEmpty(message = "目录ID不能为空")
                                 @PathVariable(value = "catalogIds") List<String> catalogIds) {
        catalogService.deleteCatalog(catalogIds);
        return R.ok();
    }
}
